package com.tianyang.yukuaifu.business.main.view;

import android.content.Context;
import android.content.Intent;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.tianyang.yukuaifu.R;
import com.tianyang.yukuaifu.base.BaseTitleActivity;
import com.tianyang.yukuaifu.business.main.adapter.ChooseSoundAdapter;
import com.tianyang.yukuaifu.business.main.bean.DeviceOutput;
import com.tianyang.yukuaifu.business.main.contract.ChooseSoundContract;
import com.tianyang.yukuaifu.business.main.contract.MainConfig;
import com.tianyang.yukuaifu.business.main.presenter.ChooseSoundPresenter;
import com.tianyang.yukuaifu.module.widget.VerticalSwipeRefreshLayout;

import java.util.List;

import butterknife.BindView;

/**
 * Description：选择音响
 * Created by xyj on 2024/4/24
 */
public class ChooseSoundActivity extends BaseTitleActivity implements ChooseSoundContract.View {

    @BindView(R.id.swipeRefreshLayout)
    VerticalSwipeRefreshLayout swipeRefreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    private ChooseSoundAdapter vChooseSoundAdapter;

    private ChooseSoundContract.Presenter mPresenter;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_recycler_view;
    }

    @Override
    protected void initView() {
        super.initView();
        commonTitle.setTitle("选择音响");
        vChooseSoundAdapter = new ChooseSoundAdapter();
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        recyclerView.setAdapter(vChooseSoundAdapter);

        mPresenter = new ChooseSoundPresenter(this);
        mPresenter.getDeviceList();
    }

    @Override
    protected void setListener() {
        super.setListener();
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPresenter.getDeviceList();
            }
        });
        vChooseSoundAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                DeviceOutput output = ((ChooseSoundAdapter)adapter).getItem(position);
                if (output != null) {
                    Intent intent = new Intent();
                    intent.putExtra(MainConfig.INTENT_DEVICE_OUTPUT, output);
                    setResult(RESULT_OK, intent);
                    finish();
                }
            }
        });
    }

    @Override
    public void showDeviceListView(List<DeviceOutput> outputs) {
        if (swipeRefreshLayout.isRefreshing())
            swipeRefreshLayout.setRefreshing(false);
        if (outputs != null) {
            vChooseSoundAdapter.setNewData(outputs);
        }
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setPresenter(ChooseSoundContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
